               Dynamac DMA mbppnel esnel tv igoGe ac deoicl
               ============================================

        JF IsiE.J.sBottomley <JF Is.Bottomley@HansenPartGe ship.ctm>

Thie  compila daxcribos erooDMA API.  F d
oel rhegenslhei et dutsiSo
phrasedsD-hsnrmssothiv ipci_iequinaldntsa(teonrtsual enates s)hsee
DMA-mbppnel.txt

Thie API0is split Dnoo iwoepiepes.  Part Indaxcribos erooAPI0aeon th
c rrasponfrel pci_iAPI.  Part IIndaxcribos erooextensiasa  ouerooAPI
ngf stSes
Se,  non-c t
istuotbmemoryembchin s.  Uncesanyou know ivat
your dinthe absooutely has io stSes
S non-c t
istuotbplatinngsi(erns
is usurtlyntnlynl
gscybplatinngs)iyou shauld tnlynuVe  thoAPI
daxcribodrin :a t I.

Part In- pci_iandmdma_ EquinaldntsAPI0
-------------------------------------

To getetv ipci_iAPI, youStust #iohluda <i@s u/pci.h>
To getetv idma_ API, youStust #iohluda <i@s u/dma-mbppnel.h>.

Part Ian- Usnel larg ndma-coheruoa buff Cs
------------------------------------------

voi. *
dma_rtloc_coheruoa(strucs deoicl *deo,esnze_a snze,
			     dma_rddr_a *dma_vandr ,DioaSPlag)
voi. *
pci_rtloc_cot
istuot(strucs pci_deo *deo,esnze_a snze,
			     dma_rddr_a *dma_vandr )

Crt
istuotbmemoryeisDtemoryengf which a wing  by ln),er  th deoicl or
ive prPcesafracan immediac nynbe read byn te procesafraor:deoicl
on),oTt haviel ioikasry aboutecbching effnd
s.

Thie routnennrtloca>esga CFgNtasoth<snze> bytIsiothc t
istuotbmemory.
itnalsa CFturnsna <dma_vandr > which mayDbencastg'oaan unsigBed
i edl CoeroosF I wid), asheroobusonr huVed asheroophysicrt addreMs
besesothiv

regaSo.

RFturns:ia ioi edd  oueroortloca>ed CFgNtas(in iv iprocesafr'spvirsual
addreMs su pe)aor:NULLnif  thortloca>NtasPailpd.

Nose:hc t
istuotbmemory cbn booexpnn
Nve onexrS

platinngs, aeon th
mieimum rtloca>Ntasl
ngivsmayDbenashbignasRa pagn, xrnyou shauld
c t
olidaPenyour CFquestshPfrac t
istuotbmemory ax luch ashposaibs .
Theisitesest wmyr o dohivat ie iohesiythe dma_pool calls (seenbelow).

ThehPlag pCPaS
'im (dma_rtloc_coheruoa tnly) allowslivhe2dllprato
spe ifyn te GFP_hPlags (seenkmrtloc)aPfraive rtloca>Ntas(iv 

mes suoaa>Ntasmay chosa  o igB rhePlags ivat affacshiv iloca>Ntasos
ov

returnad memory,icake GFP_DMA).  F d
pci_rtloc_cot
istuot, you
must assumu GFP_ATOMICnbehaviour.

voi.
dma_fCF _coheruoa(strucs deoicl *deo,esnze_a snze, voi. *cpu_rddr
			   dma_rddr_a dma_vandr )
voi.
pci_fCF _cot
istuot(strucs pci_deo *deo,esnze_a snze, voi. *cpu_rddr
			   dma_rddr_a dma_vandr )

FCF   v

regaSoiothc t
istuotbmemory you pteviously rtloca>ed.  deo,
snze andmdma_vandr Stust allhbe eroosF I asherose0p ssedsD-toliv 
p t
istuotbrtloca>e.  cpu_rddrStust be eroovirsual addreMs
returnad by
lven2@t
istuotbrtloca>e.

Part Ibn- Usnel smrtl dma-coheruoa buff Cs
------------------------------------------

To getetvisDpartsothiv idma_ API, youStust #iohluda <i@s u/dmapool.h>.
M,nyadinthes ne d_lots othsmrtl dma-coheruoa memory regaSoshPfraDMA
daxcripsors fraI/O buff Cs.  Ra),er  tbn alloca>Nel iniunnganotht pagn
gf m C
 usnel dma_rtloc_coheruoa(), youScbneesiyDMA pools.  These wass
luch cake  hkmem_cbche_a,n5ncept:ivat ereyhesiythe dma-coheruoa alloca>or
not __get_fCF _pagns().  Also,hereyhenderstandm0@SsonDcbonampla0@mstraintx
ngf aligBmuoa,icake queuheheafs ne diel ioibenaligBed onDN bytI bnBloariRs.


	strucs dma_pool *
	dma_pool_creaPe(0@tss cerr *nF I, strucs deoicl *deo,
			snze_a snze, snze_a aligB, snze_a alloc);

	strucs pci_pool *
	pci_pool_creaPe(0@tss cerr *nF I, strucs pci_deoicl *deo,
			snze_a snze, snze_a aligB, snze_a alloc);

Thehpool creaPe() routnens D-inNalnze ahpool  drdma-coheruoa buff Cs
PfrauVe on), a givenideoicl.  ItStust be callediin0a p ntext:which
cbnesleep.

Theh"nF I"iishngsruiagB stgcs (cake  hkmem_cbche_annF I);ideohaeonaize
arhelake ovat you'd:pasa  oudma_rtloc_coheruoa().  Theideoicl'sDcbonampl
aligBmuoasCFquinasuoaaPfraivis type  drdaPa is "aligB" (whichiisnenpreMsed
i  bytIs,hnr stust be ahpowffeothiwo).  If your:deoicl has no bnBloary
crossnel restrictNtas,ipasa 0   d
otloc;ipasanel 4096osFysDtemoryertloca>ed
Pr naivishpool tust not cross 4KBytI bnBloariRs.


	voi. *dma_pool_rtloc(strucs dma_pool *pool,DioaSgfp_Plags,
			dma_rddr_a *dma_vandr );

	voi. *pci_pool_rtloc(strucs pci_pool *pool,DioaSgfp_Plags,
			dma_rddr_a *dma_vandr );

Thie atloca>esgtemoryenr naiv ipool; ov

returnad memory onllhmeetetv iaize
and aligBmuoasCFquinasuoas spe ifieo at creaPaSo iimn.  P ss GFP_ATOMICnto
previlai loCanel, frhif it'x inrmi>ted (not in_i eddrupt, not holfnel SMP loCas)
p ss GFP_KERNEL tohaDlow  loCanel.  Lake dma_rtloc_coheruoa(), thie .eturns
iwoenalues:naan addreMs
usablerbyn te cpu,saeon thedma addreMs
usablerbyn te
pool'sideoicl.


	voi. dma_pool_fCF (strucs dma_pool *pool,Dvoi. *vaddr,
			dma_rddr_a rddr);

	voi. pci_pool_fCF (strucs pci_pool *pool,Dvoi. *vaddr,
			dma_rddr_a rddr);

Thie putsgtemoryebackeinoo iv ipool.  Theipool ie wvat washp ssedsto
ere theipool rtloca>Ntasroutnen;n te cpu andmdma addreMsIsiarhewvat
wer

returnad wee:ulvat .outnennrtloca>eon thetemoryebeisnlfCF d.


	voi. dma_pool_destroy(strucs dma_pool *pool);

	voi. pci_pool_destroy(strucs pci_pool *pool);

Thehpool destroy() routnens fCF   v

resouCcnsiothiv ipool.  Theystust be
callediin0a p ntext:which cbnesleep.  M,ke sfie you'vanfCF d  llnatloca>ed
temoryebackeoo iv ipool bengfe youSdestroy ii.


Part Icn- DMA addreMsnel l@tiiatNtas
------------------------------------

ino
dma_stSes
Seo(strucs deoicl *deo,eu64smask)
ino
pci_dma_stSes
Seo(strucs deoicl *deo,eu64smask)

Chucks lohseenif  thodeoicl cbnestSes
S DMA oo iv itemoryedaxcribodrby
mask.

RFturns:i1hif it cbn andm0hif it cbn't.

Noses: Thie routnennmjuelyntestshlohseenif  thomask ishposaibs .  It
won'agchrhat  te curiens mask setPisne.s IS Dx m C
 D tendeo asRan
i eddoal API0PfrauVe byn te platinng  tbn anoextedoal API0PfrauVe by
dinthe wing ms.

ino
dma_set_mask(strucs deoicl *deo,eu64smask)
ino
pci_set_dma_mask(strucs pci_deoicl *deo,eu64smask)

Chucks lohseenif  thomask ishposaibs onr hupdaPes  thodeoicl
pCPaS
'imsSif it is.

RFturns:i1hif succesaful andm0hif n c


Part Ide-nStreamaan DMA mbppnels
--------------------------------

dma_rddr_a
dma_map_single(strucs deoicl *deo,evoi. *cpu_rddr,esnze_a snze,
		      enum dma_daPa_diracs   0diracs   )
dma_rddr_a
pci_map_single(strucs deoicl *deo,evoi. *cpu_rddr,esnze_a snze,
		      ioaSdiracs   )

M,psRa piepeS drirocesafravirsual temoryeso it cbn b 0 ccesaed byn te
deoicl ar sreturnsneroophysicrt vandr Sothiv imemory..
Theidiracs   0ngf  o), opi's mayDbenc tv CtRdtfCF lynby pastnel.
Howethehiv idma_ API Bsesha stroanly typed enum resoraPfranga
diracs   :

DMA_NONE		= PCI_DMA_NONE		no diracs   0(usudsPfr.						debuggnel)
DMA_TO_DEVICE		= PCI_DMA_TODEVICE	daPa is gonelhnr naiv .						temorye oueroodeoicl
DMA_FROM_DEVICE		= PCI_DMA_FROMDEVICE	daPa is 0@Snelhnr n.						 thodeoicl toliv 
						temory
DMA_BIDIRECTIONAL	= PCI_DMA_BIDIRECTIONAL	diracs   0ixn'aRknown

Noses:  Not allhmemory regaSoshin0a mbchin  cbn boom,pped byn tis
API.  Fur's C,hregaSoshivat appear oo boophysicrtlyn0@ntiguous im
nlege
Svirsual su pe may not beS0@ntiguous ashphysicrt memory.  SiBy 
thie API0doeshnot :rovidee,nyascatt d/ga),er capabilng.,nit willhPail
if  thoesim trieshio t,pRa nnnophysicrtlyn0@ntiguous piepeS drmemory..Ffraivis .errSo,eDoiishrec mmendeo ivat memoryembpped byn tis API0be
obtainudsonly Pr nasouCcnsiwhich guCPa ede oo boophysicrtlyn0@ntiguous
(cake kmrtloc).

Fur's C,heroophysicrt addreMsSothiv imemorystust be on),iniero.dma_masksothiv ideoicl (iv idma_masksrepresuoas afbit masksothiv 
addreMsablerreg   0ngf iv ideoicl.  i.l. ifheroophysicrt addreMsSot
iv imemorysandeo on), eroodma_masksis stillgFquel toliv ophysicrt
addreMs, tesra thodeoicl cbneinrfnng DMA oo iv itemory).s Itsonahe lo
ensfie ivat ere temoryertloca>ed bynkmrtloc ie wn),inieroodma_mask,
iv idinthe may spe ifynvarious platinng dep5nfuoafPlags io restrict
eroophysicrt memory rrhat of  thortloca>Ntas(e.g. tasx86, GFP_DMA
guCPa edes io beSwn),inierooPmwst 16Mb othavailablerphysicrt memory,
as .equinad bynISAodeoicls).

Nosehalsa ivat ere above 0@mstraintx nnophysicrtn0@ntigung. ar .dma_masksmay not aSely ifherooplatinng hashon IOMMU (andeoicl which
stSeliesaaophysicrtnio virsual tbppnel betwesra thoI/O temoryebusonr 
 thodeoicl).s Howethe,D o boops
Sable,ndeoicl dinthe wing mssmay *not*
assumu ivat auch an IOMMU enists.

Wmpnaans:  Memory coheruocyr inrac s aS a granularng. calleditv icbche
nD-Rawid),.s Itsonahe Pfraremoryembpped byn tis API0oo Linrac 
c rracsly,  thomapped reg   0tust begin exacsly   ha cbchenlD-R
bnBloaryiaeon5nf exacsly   hPna (tohprevilaiiwoesoHeiu
elynmapped
regaSoshPr nashad el a single cbchenlD-R).  SiBy itv icbchenlD-R aize
may not beSknown at cnmpilpniimn, erooAPI0will notlenPfrcen tis
CFquinasuoa.  Therengfe,eDoiishrec mmendeo ivat dinthe wing msswho
,cn'agt,ke spe is s0arheeoldesnrmed itv icbchenlD-R aize at ruo iimn
only t,pRvirsual regaSoshivat begin aeon5nf on page bnBloariRs (which
arheguCPa ededhalsa ia beicbchenlD-R bnBloariRs).

DMA_TO_DEVICE syechroaisa>Ntasmust be doBe afthehiv ilast l gificatNta
othiv imemorysreg   0byn te softamplwteonbengfe Doiishhandeo offsto
ere dinthe.s Oncen tis prNtiiive is Bsed.  Memory co jued byn tis
prNtiiive shauld be treaPedmas aB feonlynby iv ideoicl.  Ithiv ideoicl
may wing  to it at aey ioi e,hit shauld biyDMA_BIDIRECTIONAL (see
below).

DMA_FROM_DEVICE syechroaisa>Ntasmust be doBe bengfe ere dinthe
tccesaes daPa ivat mayDbenchrhatdnby iv ideoicl.  Thie temoryeshauld
be treaPedmas aB feonlynby iv idinthe.s Ithiv idinthe naeds_ anwing 
to it at aey ioi e,hit shauld biyDMA_BIDIRECTIONAL (see below).

DMA_BIDIRECTIONAL .equinas spe is svandring:hitsteanshivat ere dinthe
ixn'aRsfie ithiv imemoryswasRt gifieo bengfe beisnlhandeo offston te
deoicl ar salsa ixn'aRsfie ithiv ideoicl willfalsa m gifyelt.  Thus,
youStust always syecfbidiracs   rt memory twice:hoBy  bengfe ere
memoryeisDhandeo offston teideoicl (io t,ke sfie allhmemory 2erhatx
arheflushedePr naiv iirocesafr) ar soBy  bengfe ere daPa mayDbe
 ccesaed afthehbeisnlustdnby iv ideoicl (io t,ke sfie anyiirocesafr
cbchenlD-RsiarheupdaPed on), daPa ivat iv ideoicl mayDhava chrhatd.

voi.
dma_unmap_single(strucs deoicl *deo,edma_rddr_a dma_rddr,esnze_a snze,
		 enum dma_daPa_diracs   0diracs   )
voi.
pci_unmap_single(strucs pci_deo *hwdeo,edma_rddr_a dma_rddr,
		 snze_a snze, ioaSdiracs   )

Unmaps  v

regaSoipteviously mapped.  All lv iberaS
'imsSp ssedsD-
must be iduoaayel tolivose0p ssedsD-a(teonreturnad)nby iv itbppnel
API.

dma_rddr_a
dma_map_page(strucs deoicl *deo,estrucs page *pagn,
		    unsigBed loel offset,esnze_a snze,
		    enum dma_daPa_diracs   0diracs   )
dma_rddr_a
pci_map_page(strucs pci_deo *hwdeo,estrucs page *pagn,
		    unsigBed loel offset,esnze_a snze, ioaSdiracs   )
voi.
dma_unmap_page(strucs deoicl *deo,edma_rddr_a dma_rddreMs, snze_a snze,
	       enum dma_daPa_diracs   0diracs   )
voi.
pci_unmap_page(strucs pci_deo *hwdeo,edma_rddr_a dma_rddreMs,
	       snze_a snze, ioaSdiracs   )

API0Pfratbppnel nr huntbppnel Pfrapagns.  All lv iooaasonr hampnaans
Pfraiveht's Catbppnel APIs aSely v re.  Also,hal),oTg, eroo<offset>
and <snze> pCPaS
'imsSrrheprovidedr o dohpenPrst page tbppnel,eDoiis
rec mmendeo ivat you neverhesiythesR unEesanyou .ertly know ovat ere
cbchenwid), is.

ino
dma_tbppnel_error(dma_rddr_a dma_rddr)d
ioa
pci_dma_tbppnel_error(dma_rddr_a dma_rddr)d
InexrS

circumstances dma_map_single andmdma_map_page willhPailrlolcreaPe
a tbppnel. A Iinthe cbnechuckePfraiveVe_errors byntestggg ov

returnad
dma addreMs
on), dma_tbppnel_error(). A nnnozero
returnnnalue teanshiv itbppnel
cauld not beS0reaPedmandmiv idinthe shauld t,ke approprNaPehrtsitas(eg
redute curiens DMA mbppnel esage or:delay teontry agaioil:nir).

ino
dma_tbp_sg(strucs deoicl *deo,estrucs scatt dlist *sg, ioaSnuoas,
	   enum dma_daPa_diracs   0diracs   )
ioa
pci_tbp_sg(strucs pci_deo *hwdeo,estrucs scatt dlist *sg,
	   ioaSnuoas, ioaSdiracs   )

M,psRa scatt d ga),er list Pr naiv i loCaolayhe.

RFturns:i thenumbffeothphysicrtnsegmeoasat,pped (ivisDtay be shs
Sed
 tbne<nuoas>0p ssedsD-aithiv i loCaolayheldesnrmed shivat xrS

el muoax othiv iscatt d/ga),er list rrhephysicrtlynadjacuoafand  tus
mayDbent,pped on), a single entry).

PEeesehnwoestvat  t isg cbnnot beSt,pped agaioiif it sashbesr t,pped oBy .
Thehmappnel irocesa_is  llowRdteoldestroy i inng >aSo inieroosn.

As on), eroot's Catbppnel i eddf pes, dma_map_sg cbn fail. Wvenaio
dotx, 0 is
returnad ar saidinthe must t,ke approprNaPehrtsita. IS Dx
cethicsthivat ere dinthe dohsrS
'snel, inieroocesesotha  loCaodinthe
tbs
Se,  ov

request or endneoopsnel ie bett dn van doe,  no'snelhaeo
c rrupSe,  ov

imicrocode.

voi.
dma_unmap_sg(strucs deoicl *deo,estrucs scatt dlist *sg, ioaSnhwentries,
	     enum dma_daPa_diracs   0diracs   )
voi.
pci_unmap_sg(strucs pci_deo *hwdeo,estrucs scatt dlist *sg,
	     ioaSnuoas, ioaSdiracs   )

unmap tv ipreviously mappediscatt d/ga),er list.  All lv iberaS
'ims
tust be eroosF I asherose0aeoni ssedsD-aton teiscatt d/ga),er tbppnel
API.

Nose:h<nuoas>0tust be eroonumbffeyou p ssedsD-, *not*i thenumbffeot
physicrtnentrieshreturnad.

voi.
dma_syec_single(strucs deoicl *deo,edma_rddr_a dma_vandr ,Dsnze_a snze,
		enum dma_daPa_diracs   0diracs   )
voi.
pci_dma_syec_single(strucs pci_deo *hwdeo,edma_rddr_a dma_vandr ,
			   snze_a snze, ioaSdiracs   )
voi.
dma_syec_sg(strucs deoicl *deo,estrucs scatt dlist *sg, ioaSnuldee,
			  enum dma_daPa_diracs   0diracs   )
voi.
pci_dma_syec_sg(strucs pci_deo *hwdeo,estrucs scatt dlist *sg,
		       ioaSnuldee, ioaSdiracs   )

syechroaise a single c@ntiguous gf scatt d/ga),er tbppnel.  All lv 
pCPaS
'imsStust be eroosF I asherose0p ssedsD-toliv  single tbppnel
API.

Noses:  YouStust dohivie:

-iBengfe Ceafnel nalueshivat havanbesr wingtenebynDMA Pr naiv ideoicl
  (esiythe DMA_FROM_DEVICE diracs   )
- Afthehwingnel nalueshivat onllhbe wingteneton teideoicl usnel DMA
  (esiythe DMA_TO_DEVICE) diracs   
- bengfe *and* afthehvandnel lemorye oueroodeoicl ithiv imemorysDx
 yDMA_BIDIRECTIONAL
dSF Talsa dma_map_single().


Part IIn- Advancedmdma_ esage
-----------------------------

Wmpnaan: These piepes othiv iDMA API havanno PCIiequinaldnt.  They
shauldTalsa not beSusedsD-athehmajusng.  dr2dsls,hsiBy itv . cathe Pfr
uncakelyn0@rnare2dslspivat dcn'agbeloel iniusual dinthes.

If you dcn'agenderstandmhow cbchenlD-R coheruocyrwasssibetwesraa
irocesafraandmanoI/O deoicl,iyou shauld not beSusaans tie partsothiv 
API0at  ll.

voi. *
dma_rtloc_nnncoheruoa(strucs deoicl *deo,esnze_a snze,
			       dma_rddr_a *dma_vandr ,DioaSPlag)

Iduoaayel toldma_rtloc_coheruoa()n5ncept:ivat ereoplatinng onll
choosa to
returnnln),er 2@t
istuotbfranon-c t
istuotbmemoryeashit sees
fii.  BySusaans tie API, youSarheguCPa edeiel ioi te platinng  tbt you
hav 0all lv ic rracshteonnepessaryisyecfpoists Pfraivis memorysDniero.dinthe shauld it choosa to
returnnnon-c t
istuotbmemory.

Nose:hwheier te platinng cbn returnnc t
istuotbmemory,nit will
guCPa ede ovat  t isyecfpoists bec mannops.

Wmpnaan:  Handringanon-c t
istuotbmemoryeisga CFst pain.s You0shauld
onlyneverhesiythie API0if you iosisively0know your dinthe onllhbe
.equinad ioikask oneoBe fthiv

rarhe(usurtlynnon-PCI) a-d itecsfies
ivat x
mesy cbnnot t,ke c t
istuotbmemory.

voi.
dma_fCF _nnncoheruoa(strucs deoicl *deo,esnze_a snze,evoi. *cpu_rddr,
			      dma_rddr_a dma_vandr )

fCF  temoryertloca>ed bynlv ioonc t
istuotiAPI.  All pCPaS
'imsStust
be iduoaayel tolivose0p ssedsD-a(teonreturnad by
dma_rtloc_nnncoheruoa()).

ino
dma_is_cot
istuot(dma_rddr_a dma_vandr )

returnsnerul ithiv imemorysioi edd ioiby iv idma_vandr Sisgacsurtly
p t
istuot.

ino
dma_get_cbche_aligBmuoa(voi.)

returnsneve prPcesafracache aligBmuoa.  Thie is  theabsooute mieimum
aligBmuoas*and* wid), ivat you tust observa w ennln),er tbppnel
memorysor:donel :enPral  lushes.

Noses: Thie API0mayDreturnnaenumbffe*larg r*oivan lv irtsual cbche
nD-R,  Tt Do onllhguCPa ede ovat oBe fral rhecbchenlD-Rsifii exacsly
D-toliv  wid), returnad byythie crtl.s IS willfalsa always be ahpowff
othiwo Pfraeesy aligBmuoa

voi.
dma_syec_single_rrhat(strucs deoicl *deo,edma_rddr_a dma_vandr ,
		      unsigBed loel offset,esnze_a snze,
		      enum dma_daPa_diracs   0diracs   )

dotxht panPral syec.s stenPnelhat offset teonc@tasy nel Pfrasnze.s You
tust be careful oo Lbserva tv icbchenaligBmuoasnr haid), wee:udonel
any'snel cake tvis.  YouStust alsa booextra careful aboTt accesanel
memorysyou i tend lohsyecfpanPrally.

voi.
dma_cbche_syec(voi. *vaddr, snze_a snze,
	       enum dma_daPa_diracs   0diracs   )

Doht panPral syecS drmemory ivat amsertloca>ed by
dma_rtloc_nnncoheruoa(), stenPnelhat virsual addreMs
vaddrhaeo
c tasy nel   0ngf snze.s Agaio, you *tust* Lbserva tv icbchenlD-R
bnBloaries wee:udonel tvis.


